package edu.clemson.wcbeck.cft;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.channels.FileChannel;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;

public class CFTAbout extends Activity{

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.about);     
    }
	
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.aboutmenu, menu);
        return true;
    }
    
    public void initMain(){
    	Intent intent = new Intent();
        setResult(RESULT_OK, intent);
        finish();
    }
    
    public void initBackup(){
//    	String toastText = "Trying to do something!";
//		Toast myToast = Toast.makeText(this.getApplicationContext(), toastText, Toast.LENGTH_SHORT);
//		myToast.show();
    	
//		ExportDatabaseFileTask()
//    	ExportDatabaseFileTask exportDB = new ExportDatabaseFileTask(getBaseContext());
    	
    	try {
            File sd = Environment.getExternalStorageDirectory();
            File data = Environment.getDataDirectory();

            if (sd.canWrite()) {
                String currentDBPath = "//data//edu.clemson.wcbeck.cft//databases//CFTDatabaseFixed.db";
                String backupDBPath = "cftDataBase.db";
                File currentDB = new File(data, currentDBPath);
                File backupDB = new File(sd, backupDBPath);

                if (currentDB.exists()) {
                    FileChannel src = new FileInputStream(currentDB).getChannel();
                    FileChannel dst = new FileOutputStream(backupDB).getChannel();
                    dst.transferFrom(src, 0, src.size());
                    src.close();
                    dst.close();
                    Toast.makeText(getBaseContext(), backupDB.toString(), Toast.LENGTH_LONG).show();
                }
            }
        } catch (Exception e) {
        	Toast.makeText(getBaseContext(), e.toString(), Toast.LENGTH_LONG).show();
        }
    }
    
    /*public void initExit(){
    	Intent intent = new Intent(this, FinActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    	startActivity(intent);
    	finish();
    }*/
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.icBackup: initBackup();
                                break;
            case R.id.icBack: initMain();
            					break;
            //case R.id.icExit: initExit();
            //					break;
        }
        return true;
    }
}
